package com.n.application;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.n.dao.CodeValueDao;
import com.n.dao.CustomerDao;
import com.n.dao.SupplierDao;
import com.n.model.CodeValue;
import com.n.model.Customer;
import com.n.model.Supplier;
import com.n.util.Constant;

public class SystemCache {
	
	public static List<CodeValue> CUSTOMER_AREA_LIST = new ArrayList<CodeValue>();
	public static Map<Integer,String> CUSTOMER_AREA_MAP = new HashMap<Integer, String>();
	
	public static List<Customer> CUSTOMER_LIST = new ArrayList<Customer>();
	public static List<Supplier> SUPPLIER_LIST = new ArrayList<Supplier>();

	
	public static List<CodeValue> SUPPLIER_TYPE_LIST = new ArrayList<CodeValue>();
	public static Map<Integer,String> SUPPLIER_TYPE_MAP = new HashMap<Integer, String>();
	
	
	public static void buildCodeValue(CodeValueDao codeValueDao){
		CUSTOMER_AREA_LIST = codeValueDao.loadByType(Constant.CVTYPE_CUSTOMER_AREA);
		SUPPLIER_TYPE_LIST = codeValueDao.loadByType(Constant.CVTYPE_SUPPLIER_TYPE);
		
		listToMap(CUSTOMER_AREA_LIST,CUSTOMER_AREA_MAP);
		listToMap(SUPPLIER_TYPE_LIST,SUPPLIER_TYPE_MAP);		
	}
	
	public static void buildCustomer(CustomerDao customerDao){
		CUSTOMER_LIST = customerDao.loadAll(Customer.class);
	}
	
	public static void buildSupplier(SupplierDao supplierDao){
		SUPPLIER_LIST = supplierDao.loadAll(Supplier.class);
	}
	
	private static void listToMap(List<CodeValue> list, Map<Integer,String> target){
		for (CodeValue c : list) {
			target.put(c.getId(), c.getValue());
		}
	}
}
